package com.imooc.course.business.controller.admin;

import com.imooc.course.business.form.TeacherForm;
import com.imooc.course.business.form.PageForm;
import com.imooc.course.server.dto.TeacherDto;
import com.imooc.course.server.dto.PageDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.service.TeacherService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/teacher")
public class TeacherController {

    private static final Logger log = LoggerFactory.getLogger(TeacherController.class);
    public static final String BUSINESS_NAME = "讲师";


    @Resource
    private TeacherService teacherService;

    /**
     * 查询列表
     * @param pageForm 传入的page和size
     * @return
     */
    @PostMapping("/list")
    public ResponseDto getList(@RequestBody PageForm pageForm) {
        PageDto pageDto = CopyUtil.copy(pageForm, PageDto.class);
        teacherService.getList(pageDto);
        return ResponseDto.success().setContent(pageDto);
    }

    @PostMapping("/all")
    public ResponseDto getAll() {
        return ResponseDto.success().setContent(teacherService.getAll());
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param teacherForm
     * @return
     */
    @PostMapping("/save")
    public ResponseDto addTeacher(@RequestBody TeacherForm teacherForm) {
        //入参检查
        ValidatorUtil.notNull(teacherForm.getName(), "姓名");
        ValidatorUtil.minimumToMaximum(teacherForm.getName(), "姓名", 1, 50);
        ValidatorUtil.minimumToMaximum(teacherForm.getNickname(), "昵称", 1, 50);
        ValidatorUtil.minimumToMaximum(teacherForm.getImage(), "头像", 1, 100);
        ValidatorUtil.minimumToMaximum(teacherForm.getPosition(), "职位", 1, 50);
        ValidatorUtil.minimumToMaximum(teacherForm.getMotto(), "座右铭", 1, 50);
        ValidatorUtil.minimumToMaximum(teacherForm.getIntro(), "简介", 1, 500);

        TeacherDto teacherDto = CopyUtil.copy(teacherForm, TeacherDto.class);
        teacherService.save(teacherDto);
        return ResponseDto.success().setContent(teacherDto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto del(@PathVariable String id) {
        teacherService.delete(id);
        return ResponseDto.success();
    }
}
